package com.iflytek.autofly.setting.biz.system;

import android.content.Context;
import android.content.Intent;
import android.content.pm.IPackageDataObserver;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.os.PowerManager;

import com.iflytek.autofly.utils.FlyLog;

import java.util.List;

/**
 * <b>Description: </b>TODO<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2015/11/26</td>
 * <td>17:08</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
public class SystemControl {
    private static final int CACHE_CLEAR = 0x1001;
    private static final java.lang.String TAG = "SystemControl";
    private final Context mContext;
    private ClearCacheObserver mClearCacheObserver = new ClearCacheObserver();
    private HandlerThread mClearHandlerThread;
    private Handler mClearHandler;
    private ClearProgressCallback mClearProgressCb;
    private List<PackageInfo> mPackageInfoList;
    private int mClearIndex;

    public SystemControl(Context context) {
        mContext = context;
        mClearHandlerThread = new HandlerThread("fly_clear");
        mClearHandlerThread.start();
        mClearHandler = new Handler(mClearHandlerThread.getLooper(), new Handler.Callback() {
            public boolean handleMessage(Message msg) {
                if (msg.what == CACHE_CLEAR) {
                    if (mClearIndex >= mPackageInfoList.size()) {
                        mClearProgressCb.onProgressChanged(100);
                        mClearProgressCb.onClearEnd();
                        return true;
                    } else {
                        mClearProgressCb.onProgressChanged(mClearIndex * 100 / mPackageInfoList.size());
                    }
                    FlyLog.d(TAG, "clearing " + mClearIndex + "/" + mPackageInfoList.size());

                    PackageInfo pkgInfo = mPackageInfoList.get(mClearIndex);
                    PackageManager pm = mContext.getPackageManager();
                    pm.deleteApplicationCacheFiles(pkgInfo.packageName, mClearCacheObserver);
                }
                return true;
            }
        });
    }

    public void restoreFactory() {
        //mContext.sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
        mContext.sendBroadcast(new Intent("autofly.intent.action.MASTER_CLEAR"));
    }

    public void reboot() {
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        pm.reboot(null);
    }

    public void sweep(ClearProgressCallback cb) {
        mClearProgressCb = cb;
        PackageManager pm = mContext.getPackageManager();
        mPackageInfoList = pm.getInstalledPackages(0);
        mClearIndex = 0;
        Message clearMsg = new Message();
        clearMsg.what = CACHE_CLEAR;
        mClearHandler.sendMessage(clearMsg);
    }

    public interface ClearProgressCallback {
        void onProgressChanged(int progress);

        void onClearEnd();
    }

    class ClearCacheObserver extends IPackageDataObserver.Stub {
        public void onRemoveCompleted(final String packageName, final boolean succeeded) {
            FlyLog.d(TAG, ": " + mClearIndex);
            mClearIndex++;
            Message clearMsg = new Message();
            clearMsg.what = CACHE_CLEAR;
            mClearHandler.sendMessage(clearMsg);
        }
    }
}
